package com.handwriting.backend.controller;

import com.handwriting.backend.entity.User;
import com.handwriting.backend.exceptions.PasswordException;
import com.handwriting.backend.exceptions.UserAlreadyExistsException;
import com.handwriting.backend.exceptions.UserNotFoundException;
import com.handwriting.backend.message.LoginInfo;
import com.handwriting.backend.message.RegisterInfo;
import com.handwriting.backend.message.Response;
import com.handwriting.backend.message.UserInfo;
import com.handwriting.backend.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
public class UserController {
    private final UserService service;

    public UserController(UserService service) {
        this.service = service;
    }

    @Operation(summary = "用户登录")
    @PostMapping(value = "/user/login")
    public Response<String> login(@RequestBody LoginInfo loginInfo) {
        try {
            String token = service.login(loginInfo.getUsername(), loginInfo.getPassword());
            return Response.ok(token);
        } catch (UserNotFoundException e) {
            e.printStackTrace();
            return Response.error("User does not exist", Response.UserNotFoundError);
        } catch (PasswordException e) {
            e.printStackTrace();
            return Response.error("Password is error.", Response.PasswordError);
        } catch (Exception e) {
            e.printStackTrace();
            return Response.error("Unexpected error.");
        }
    }

    @Operation(summary = "注册用户")
    @PostMapping(value = "/user/register")
    public Response<Object> register(@RequestBody RegisterInfo regInfo) {
        try {
            service.register(regInfo.getUsername(), regInfo.getPassword(), regInfo.getRole());
            return Response.ok(null);
        } catch (UserAlreadyExistsException e) {
            e.printStackTrace();
            return Response.error("User has already existed", Response.AlreadyExistError);
        } catch (Exception e) {
            e.printStackTrace();
            return Response.error("Unexpected error.");
        }
    }

    @Operation(summary = "删除用户")
    @DeleteMapping(value = "/user/{id}")
    public Response<Object> deleteUser(@PathVariable int id) {
        try {
            service.deleteUser(id);
            return Response.ok(null);
        } catch (Exception e) {
            e.printStackTrace();
            return Response.ok(null);
        }
    }

    @Operation(summary = "获取用户信息")
    @GetMapping(value = "/user/{username}")
    public Response<User> getUser(@PathVariable String username) {
        try {
            User user = service.getUser(username);
            return Response.ok(user);
        } catch (Exception e) {
            e.printStackTrace();
            return Response.error("No such user");
        }
    }

    @Operation(summary = "获取所有用户")
    @GetMapping(value = "/user/all")
    public Response<List<UserInfo>> getUsers() {
        try {
            List<UserInfo> users = service.getUsers();
            return Response.ok(users);
        } catch (Exception e) {
            e.printStackTrace();
            return Response.error("Error finding users.");
        }
    }

    @Operation(summary = "修改用户信息")
    @PutMapping(value = "/user/info")
    public Response<Object> updateUser(@RequestBody UserInfo userInfo) {
        try {
            service.updateUser(userInfo);
            return Response.ok(null);
        } catch (UserNotFoundException e) {
            e.printStackTrace();
            return Response.error("No such user", Response.UserNotFoundError);
        } catch (Exception e) {
            e.printStackTrace();
            return Response.error("Unexpected error");
        }
    }
}
